package com.sphd.emanu.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.sphd.emanu.R;
import com.sphd.emanu.interfaces.FoodNoListener;
import com.sphd.emanu.pojo.FoodEntity;


import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @Author Hu
 * @Time 2017/4/27 --16:19.
 * @Email 87683202@qq.com
 */
public class ShoppingCartAdapter extends BaseAdapter {
    Context context;
    List<FoodEntity> shopcarList;
    FoodNoListener foodNoListener;

    public void setFoodNoListener(FoodNoListener foodNoListener) {
        this.foodNoListener = foodNoListener;
    }

    public ShoppingCartAdapter(Context context, List<FoodEntity> shopcarList) {
        this.context = context;
        this.shopcarList = shopcarList;
    }

    @Override
    public int getCount() {
        return shopcarList.size();
    }

    @Override
    public Object getItem(int position) {
        return shopcarList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.item_shoppingcar, null);
            viewHolder=new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        }else {
            viewHolder= (ViewHolder) convertView.getTag();
        }
        final FoodEntity foodEntity =shopcarList.get(position);
        viewHolder.foodName.setText(foodEntity.getDishesName());
        viewHolder.shoppingNum.setText(String.valueOf(foodEntity.getChooseNo()));
        viewHolder.increase.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int num = foodEntity.getChooseNo();
                num++;
                foodEntity.setChooseNo(num);

                viewHolder.shoppingNum.setText(String.valueOf(foodEntity.getChooseNo() == 0 ? "" : foodEntity.getChooseNo()));
                if (foodNoListener != null) {
                    foodNoListener.add(foodEntity,2,position);
                }
            }
        });
        //减
        viewHolder.reduce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int num = foodEntity.getChooseNo();
                if (num > 0) {
                    num--;
                    foodEntity.setChooseNo(num);
                    viewHolder.shoppingNum.setText(String.valueOf(foodEntity.getChooseNo() == 0 ? "" : foodEntity.getChooseNo()));
                    if (foodNoListener != null) {
                        foodNoListener.reduce(foodEntity,2,position);
                    }
                }
            }
        });

        return convertView;
    }

    static class ViewHolder {
        @BindView(R.id.food_name)
        TextView foodName;
        @BindView(R.id.foo_price)
        TextView fooPrice;
        @BindView(R.id.reduce)
        TextView reduce;
        @BindView(R.id.shoppingNum)
        TextView shoppingNum;
        @BindView(R.id.increase)
        TextView increase;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
    /**
     * 局部更新数据，调用一次getView()方法；Google推荐的做法
     *
     * @param listView 要更新的listview
     * @param position 要更新的位置
     */
    public void notifyDataSetChangedItem(ListView listView, int position) {
        /**第一个可见的位置**/
        int firstVisiblePosition = listView.getFirstVisiblePosition();
        /**最后一个可见的位置**/
        int lastVisiblePosition = listView.getLastVisiblePosition();

        /**在看见范围内才更新，不可见的滑动后自动会调用getView方法更新**/
        if (position >= firstVisiblePosition && position <= lastVisiblePosition) {
            /**获取指定位置view对象**/
            View view = listView.getChildAt(position - firstVisiblePosition);
            getView(position, view, listView);
        }

    }
}
